package com.example.springboot.controller;

import com.example.springboot.dao.admin_roleMapper;
import com.example.springboot.model.RtPageInfo;
import com.example.springboot.model.admin_menu;
import com.example.springboot.model.admin_role;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class admin_roleController {
    @Autowired
    admin_roleMapper admin_roleMapper;

    @RequestMapping("/admin_role_list")
    public String listCategory() throws Exception {
        return "admin/admin_role/list";
    }
    //查询
    @RequestMapping(value = "/admin_role_query", method = RequestMethod.GET)
    @ResponseBody
    public RtPageInfo query(
            @RequestParam(required = false, name = "pageSize") Integer pageSize,
            @RequestParam(required = false, name = "startIndex") Integer startIndex,
            @RequestParam(required = false, name = "pageIndex") Integer pageIndex,
            @RequestParam(required = false, name = "name") String name){
        PageHelper.startPage(startIndex,pageSize,"id desc");
        //正常无条件检索
        //List<admin_menu> cs=admin_menuMapper.findAll();
        //多条件检索 使用map K-V参数形式
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name",name);

        List<admin_role> cs= admin_roleMapper.FindAllWhere(map);
        PageInfo<admin_role> page = new PageInfo<>(cs);
        //返回DataTable使用
        RtPageInfo pageInfo = new RtPageInfo();
        pageInfo.setData(page.getList());//这里是数据内容 List
        pageInfo.setPageNum(startIndex/pageSize);//Integer
        pageInfo.setPageSize(pageSize);//pageSize
        pageInfo.setTotalCount(page.getTotal());//BigInteger
        return pageInfo ;
    }

    @RequestMapping("/admin_role_add")
    public String  Add(){
        return "admin/admin_role/add";
    }

    @RequestMapping(value = "/admin_role_action",method = RequestMethod.POST)
    @ResponseBody
    public String  AddItem(@RequestParam(value = "name") String name,
                           HttpServletRequest request) throws IOException {
        admin_role a=new admin_role();
        a.setAddtime(new Date());
        a.setName(name);
        admin_roleMapper.save(a);
        return "{\"message\":\"ok\"}";
    }


}
